﻿using PLC;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Pumptester.test
{
    public partial class SerialFormDemo : Form
    {
        public SerialFormDemo()
        {
            InitializeComponent();
        }
        SerialPortManager serialPortManager = null;
        private void btnOpenSerialPort_Click(object sender, EventArgs e)
        {
           
            string serialName = comboBoxSerialPort.SelectedItem.ToString();
           

            //设置各“串口设置”
            string strBaudRate = comboBoxBaudRate.Text;
            string strDateBits = textBoxData.Text;
            string strStopBits = comboBoxStop.Text;
            Int32 iBaudRate = Convert.ToInt32(strBaudRate);
            Int32 iDateBits = Convert.ToInt32(strDateBits);

            StopBits stopBits = StopBits.None;
            switch (comboBoxStop.Text)            //停止位
            {
                case "0":
                    stopBits = StopBits.None;
                    break;
                case "1":
                    stopBits = StopBits.One;
                    break;
                case "1.5":
                    stopBits = StopBits.OnePointFive;
                    break;
                case "2":
                    stopBits = StopBits.Two;
                    break;
                default:
                    MessageBox.Show("Error：参数不正确!", "Error");
                    break;
            }
            Parity parity = Parity.None;
            switch (comboBoxCheck.Text)             //校验位
            {
                case "无":
                    parity = Parity.None;
                    break;
                case "奇校验":
                    parity = Parity.Odd;
                    break;
                case "偶校验":
                    parity = Parity.Even;
                    break;
                default:
                    MessageBox.Show("Error：参数不正确!", "Error");
                    break;
            }

            serialPortManager = SerialPortManager.GetInstance(serialName, iBaudRate, parity, iDateBits, stopBits);

            serialPortManager.Open();

            //设置必要控件不可用
            comboBoxSerialPort.Enabled = false;
            comboBoxBaudRate.Enabled = false;
            textBoxData.Enabled = false;
            comboBoxStop.Enabled = false;
            comboBoxCheck.Enabled = false;
        }

        private void btnSendData_Click(object sender, EventArgs e)
        {
            byte[] b = serialPortManager.readData();
            StringBuilder sb = new StringBuilder();
            //sb.Append("recieved (" + buffer + "):bytes, type:"+e.EventType+")");
            for (int i = 0; i < b.Length; i++)
            {
                sb.Append(b[i].ToString() + " ");
            }
            textBoxRecieveData.Text = sb.ToString();
        }

        private void SerialFormDemo_Load(object sender, EventArgs e)
        {

            byte[] ssdsd = new byte[] { 0x00, 0x00, 0x90, 0x02, 0x00, 0x00, 0x00, 0x00 };
            byte[] ssdsd1 = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            double doubleValue = BitConverter.ToDouble(ssdsd, 0);
            double doubleValue1 = BitConverter.ToDouble(ssdsd1, 0);

            byte[] datas = BitConverter.GetBytes(3.3f);
            byte[] bytes
                //= new byte[] { 102, 102, 102, 102, 102, 102, 10, 64 };
             = new byte[] { 51, 51, 83, 64 };

            double d = BitConverter.ToSingle(bytes, 0);

            comboBoxBaudRate.SelectedIndex = 0;
            textBoxData.Text = "8";
            comboBoxCheck.SelectedIndex = 0;
            comboBoxStop.SelectedIndex = 0;

            string[] strCom = SerialPort.GetPortNames();
            if (strCom == null)
            {
                MessageBox.Show("本机没有串口！", "Error");
                return;
            }
            //GetPortNames()方法:获取当前计算机的串行端口名的数组
            foreach (string com in System.IO.Ports.SerialPort.GetPortNames())
            {
                comboBoxSerialPort.Items.Add(com);
            }

            comboBoxSerialPort.SelectedIndex = 0;
            //sp1.BaudRate = 9600;
           // Control.CheckForIllegalCrossThreadCalls = false;
            //port.DataReceived += DataReceived;

        }
    }
}
